/**
 * Created by WT-SUN on 2017/4/10.
 */
$(function () {

    var OHE_FACILITY_TYPE=CONSTANT.DictionaryTypeDefConstants.OHE_FACILITY_TYPE;//防护设施类型
    var OHE_FACILITY_USE=CONSTANT.DictionaryTypeDefConstants.OHE_FACILITY_USE;//防护设施使用情况
    queryComboData(OHE_FACILITY_TYPE,'facilitieTypeQuery');
    queryComboData(OHE_FACILITY_USE,'usageQuery');
    $('#btn_save').on('click', function () {
        submitForm();
    });
    var overHaulData=[{id:'0',text:'定期',selected:true},{id:'1',text:'不定期'}];
    pageComData('overHaulQuery',overHaulData);
    var stateData=[{id:'0',text:'启用',selected:true},{id:'1',text:'停用'}];
    pageComData('stateQuery',stateData);
    var isIntactData=[{id:'0',text:'是',selected:true},{id:'1',text:'否'}];
    pageComData('isIntactQuery',isIntactData);

    /**
     * 绑定人员选择事件
     */
    $("#personLiable").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_personLiable_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
            }
        }]
    });
    $("#hazardFactorName").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_hazardFactor_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');//
                $('#hazardFactorId').val('');
            }
        }]
    });
    
});
var add_hazardFactor_func=function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择危害因素',
        area: ['900px', '60%'],
        content: ROOT_PATH + '/occupationalHealth/hazardFactor/listTb.shtml?isMulti=0',
        callBack:function(data){
            getSelectedFactor(data);
        }
    });
};
var getSelectedFactor = function (data) {
    if (data.length>1){
        var names = '';
        var ids='';
        for (var index in data) {
            names += ',' + data[index].name;
            ids += ',' + data[index].id;
        }
        $("#hazardFactorName").textbox('setValue', names.substring(1)).textbox('setText', names.substring(1));
        $("#hazardFactorId").val(ids.substring(1));
    }else {
        $("#hazardFactorName").textbox('setValue', data[0].name).textbox('setText', data[0].name);
        $("#hazardFactorId").val(data[0].id);
    }
};
var add_personLiable_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#personLiable").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
};
function hazardFactorData() {
    var $html = $("<ul class='autocomplete select_checkbox_div' style='top: 209px;left: 130px; border: 1px solid #3799dc;background: white;font-size: 16px;width: 597px;overflow-x:hidden;position: absolute;z-index: 99'></ul>").hide().insertAfter("#hazardFactorName");//定义一个html标签
    $('#hazardFactorName').keyup(function () {
        if ($("#hazardFactorName").val() != "") {
            var code=$("#hazardFactorName").val();
            $.ajax({
                type: 'post',
                url: ROOT_PATH + '/occupationalHealth/hazardFactor/selectFactorComboData.shtml',
                data: {name: code},
                dataType: "json",
                success: function (data) {
                    $html.empty();
                    $('#hazardFactorId').val("");
                    if (data.length > 0) {
                        $.each(data, function (i, item) {
                            var autocompleteNode = $(".autocomplete");
                            $("<li ></li>").text(item.name).appendTo(autocompleteNode).mouseover(function () {
                                $(this).css("background", "#5CACEE");
                            }).mouseout(function () {
                                $(this).css("background", "white");
                            }).click(function () {
                                $("#hazardFactorName").val(item.name);
                                $("#hazardFactorId").val(item.id);
                                autocompleteNode.hide();
                            });
                        });
                        $html.show();
                    }
                }
            });
        } else {
            $html.hide();
            $('#hazardFactorId').val("");
        }

    });
}
function pageComData(id,data) {
    var pageId='#'+id;
    $(pageId).combobox({
        data: data,
        prompt:'请选择',
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '200',
        selectOnNavigation:true,
    });
}
function submitForm() {
    if($('#hazardFactorName').val()==''){
        layer.alert('请填写危害因素名称');
        return;
    }
    if($('#hazardFactorId').val()==''){
        layer.alert('危害因素不正确');
        return;
    }

    $('#formFacility').form('submit', {
        url: ROOT_PATH + '/occupationalHealth/facility/insertSelective.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    $.getIframeWindow().reLoadData();
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
function queryComboData(code,queryId) {
    /*var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };*/
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                /*loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },*/
                data: result,
                prompt:'请选择',
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
            });
        }
    });
}